﻿using System;
using System.Data;
using System.Collections.Generic;
using LTP.Common;


namespace Usas.BLL.Cms
{
    public class AspPager
    {

        private readonly Usas.DAL.Cms.AspPager ap = new Usas.DAL.Cms.AspPager();

        public int Size()
        {
            return ap.Size();
        }

        public DataTable ShowListDesc(string table, string where, string PK, int num, int NowPage, string col)
        {
            int index = 0;
            int Count = ShowListCount(table, where, PK);
            if (NowPage >= 1)
            {
                if (Count > 0)
                {
                    index = Count - (NowPage - 1) * num;
                    if (index <= 0)
                    {
                        index = Count % num;
                    }
                }
                else
                {
                    return null;
                }
            }
            return ap.ShowListDesc(table, where, PK, num, index, col).Tables[0];
        }

        public DataTable ShowListDesc(string table, string where, string PK, int num, int NowPage, string col, string by)
        {
            int index = 0;
            int Count = ShowListCount(table, where, PK);
            if (Count == 0)
            {
                return null;
            }
            if (NowPage >= 1)
            {

                index = Count - (NowPage - 1) * num;
                if (index <= 0)
                {
                    index = Count % num;
                }
            }
            return ap.ShowListDesc(table, where, PK, num, index, col, by).Tables[0];
        }

        public int ShowListCount(string table, string where, string PK)
        {
            return ap.ShowListCount(table, where, PK);
        }

        public DataTable ShowListAsc(string table, string where, string PK, int num, int NowPage, string col)
        {
            int index = 0;
            int Count = ShowListCount(table, where, PK);
            if (Count == 0)
            {
                return null;
            }
            if (NowPage >= 1)
            {
                index = Count - (NowPage - 1) * num;
                if (index <= 0)
                {
                    index = Count % num;
                }
            }
            return ap.ShowListAsc(table, where, PK, num, index, col).Tables[0];
        }

        public int TranSql(List<string> ls)
        {
            return ap.TranSql(ls);
        }



        #region article
        public DataTable GetArticleTableBySetting(string strWhere, string CurrentTableWhere, int NowPage, int ShowNum, string Sortby)
        {
            string table = "cmsArticle";
            string where = "artid in (select p_pid from cms_purview_setting where " + strWhere + " and p_table='a')" + " and " + CurrentTableWhere;
            string pk = "ArtId";
            int page = NowPage;
            int num = 0;
            string by = Sortby;
            string cols = "ArtId,TypeId,BorderId,ParentArtId,ArtOrder,ArtDirectory,ArtTopic,ArtKeys,ArtFrom,ArtSummary,ArtFileName,ModfiyTime,hits,Modify,Writer,Auditer,Audit,ArtFormat,Version,CanView,ArtViewPurview,ArtEditPurview,ShowOrder,IsVersion,IsTop,PublishStatus," +
                "(select count(p_type) from cms_purview_setting where (" + strWhere + ") and p_type=0 and p_pid=ArtId and p_table='a') as 'view'," +
               "(select count(p_type) from cms_purview_setting where (" + strWhere + ") and p_type=1 and p_pid=ArtId and p_table='a') as 'edit'";


            if (ShowNum == -1)
            {
                num = ShowListCount(table, where, pk);
            }
            else
            {
                num = ShowNum;
            }
            return ShowListDesc(table, where, pk, num, page, cols, by);
        }

        public DataTable GetArticleTableBySetting(string strWhere, string CurrentTableWhere, int NowPage, int ShowNum, string Sortby, out int Count)
        {
            string table = "cmsArticle";
            string where = "artid in (select p_pid from cms_purview_setting where " + strWhere + " and p_table='a')" + " and " + CurrentTableWhere;
            string pk = "ArtId";
            int page = NowPage;
            int num = 0;
            string by = Sortby;
            string cols = "ArtId,TypeId,BorderId,ParentArtId,ArtOrder,ArtDirectory,ArtTopic,ArtKeys,ArtFrom,ArtSummary,ArtFileName,ModfiyTime,hits,Modify,Writer,Auditer,Audit,ArtFormat,Version,CanView,ArtViewPurview,ArtEditPurview,ShowOrder,IsVersion,IsTop,PublishStatus," +
                "(select count(p_type) from cms_purview_setting where (" + strWhere + ") and p_type=0 and p_pid=ArtId and p_table='a') as 'view'," +
               "(select count(p_type) from cms_purview_setting where (" + strWhere + ") and p_type=1 and p_pid=ArtId and p_table='a') as 'edit'";


            Count = ShowListCount(table, where, pk);
            if (ShowNum == -1)
            {
                num = Count;

            }
            else
            {
                num = ShowNum;
            }


            return ShowListDesc(table, where, pk, num, page, cols, by);
        }
        #endregion

        #region type
        public DataTable GetTypeTableBySetting(string strWhere, string CurrentTableWhere, int NowPage, int ShowNum, string Sortby)
        {
            string table = "cmsType";
            string where = "TypeId in (select p_pid from cms_purview_setting where " + strWhere + " and p_table='t')" + " and " + CurrentTableWhere;
            string pk = "TypeId";
            int page = NowPage;
            int num = 0;
            string by = Sortby;
            string cols = " TypeId,TypeOrder,TypeName,TypeKey,Purview,EditPurview,ImgFile,Del," +
                "(select count(p_type) from cms_purview_setting where (" + strWhere + ") and p_type=0 and p_pid=TypeId and p_table='t') as 'view'," +
               "(select count(p_type) from cms_purview_setting where (" + strWhere + ") and p_type=1 and p_pid=TypeId and p_table='t') as 'edit'";

            if (ShowNum == -1)
            {
                num = ShowListCount(table, where, pk);
            }
            else
            {
                num = ShowNum;
            }


            return ShowListDesc(table, where, pk, num, page, cols, by);
        }

        public DataTable GetTypeTableBySetting(string strWhere, string CurrentTableWhere, int NowPage, int ShowNum, string Sortby,out int Count)
        {
            string table = "cmsType";
            string where = "TypeId in (select p_pid from cms_purview_setting where " + strWhere + " and p_table='t')" + " and " + CurrentTableWhere;
            string pk = "TypeId";
            int page = NowPage;
            int num = 0;
            string by = Sortby;
            string cols = " TypeId,TypeOrder,TypeName,TypeKey,Purview,EditPurview,ImgFile,Del," +
                "(select count(p_type) from cms_purview_setting where (" + strWhere + ") and p_type=0 and p_pid=TypeId and p_table='t') as 'view'," +
               "(select count(p_type) from cms_purview_setting where (" + strWhere + ") and p_type=1 and p_pid=TypeId and p_table='t') as 'edit'";
            
            Count = ShowListCount(table, where, pk);
            if (ShowNum == -1)
            {
                num = Count;
                
            }
            else
            {
                num = ShowNum;
            }


            return ShowListDesc(table, where, pk, num, page, cols, by);
        }
        #endregion



        #region border
        public DataTable GetBorderTableBySetting(string strWhere, string CurrentTableWhere, int NowPage, int ShowNum, string Sortby)
        {
            string table = "cmsBorder";
            string where = "BorderId in (select p_pid from cms_purview_setting where " + strWhere + " and p_table='b')" + " and " + CurrentTableWhere;
            string pk = "BorderId";
            int page = NowPage;
            int num = 0;
            string by = Sortby;
            string cols = "BorderId,TypeId,BorderOrder,BorderName,ViewPurview,EditPurview,ImgFile,Del,IsCheck,CheckPerson," +
                "(select count(p_type) from cms_purview_setting where (" + strWhere + ") and p_type=0 and p_pid=BorderId and p_table='b') as 'view'," +
               "(select count(p_type) from cms_purview_setting where (" + strWhere + ") and p_type=1 and p_pid=BorderId and p_table='b') as 'edit'";

            if (ShowNum == -1)
            {
                num = ShowListCount(table, where, pk);
            }
            else
            {
                num = ShowNum;
            }
            return ShowListDesc(table, where, pk, num, page, cols, by);
        }

        public DataTable GetBorderTableBySetting(string strWhere, string CurrentTableWhere, int NowPage, int ShowNum, string Sortby, out int Count)
        {
            string table = "cmsBorder";
            string where = "BorderId in (select p_pid from cms_purview_setting where " + strWhere + " and p_table='b')" + " and " + CurrentTableWhere;
            string pk = "BorderId";
            int page = NowPage;
            int num = 0;
            string by = Sortby;
            string cols = "BorderId,TypeId,BorderOrder,BorderName,ViewPurview,EditPurview,ImgFile,Del,IsCheck,CheckPerson," +
                "(select count(p_type) from cms_purview_setting where (" + strWhere + ") and p_type=0 and p_pid=BorderId and p_table='b') as 'view'," +
               "(select count(p_type) from cms_purview_setting where (" + strWhere + ") and p_type=1 and p_pid=BorderId and p_table='b') as 'edit'";

            Count = ShowListCount(table, where, pk);
            if (ShowNum == -1)
            {
                num = Count;

            }
            else
            {
                num = ShowNum;
            }

            return ShowListDesc(table, where, pk, num, page, cols,by);
        }
        #endregion
    }


}
